﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AutoMapper;
using WebStore.Domain.Entities.Model;
using WebStore.Web.App.Models.Account;
using WebStore.Web.App.Models.Admin;
using WebStore.Web.App.Models.Cart;
using WebStore.Web.App.Models.Product;

namespace WebStore.Web.App
{
    public partial class Bootstrapper
    {
        private static void CreateMapping()
        {
            ProductMapping();
            AccountMapping();
            OrderMapping();
            Mapper.AssertConfigurationIsValid();
        }

        private static void ProductMapping()
        {
            Mapper.CreateMap<Product, ProductModel>()
                .ForMember(x => x.Category, opt => opt.MapFrom(from => from.Category.Name))
                .ForMember(x=>x.HasPhoto,opt=>opt.MapFrom(from=>from.Photo!=null))
                .ForMember(x => x.Quantity, opt => opt.Ignore());

            Mapper.CreateMap<Product, EditProductModel>()
                .ForMember(x=>x.Photo,opt=>opt.Ignore())
                .ForMember(x => x.Categories, opt => opt.Ignore());

        }
        private static void AccountMapping()
        {
            Mapper.CreateMap<User, ProfileModel>();

            Mapper.CreateMap<ProfileModel, User>()
                .ForMember(x => x.Email, opt => opt.Ignore())
                .ForMember(x => x.Id, opt => opt.Ignore())
                .ForMember(x => x.Password, opt => opt.Ignore())
                .ForMember(x=>x.UserRoles,opt=>opt.Ignore())
                .ForMember(x=>x.Orders,opt=>opt.Ignore());
        }
        private static void OrderMapping()
        {
            Mapper.CreateMap<KeyValuePair<int, int>, OrderDetail>()
                .ForMember(x => x.ProductId, opt => opt.MapFrom(from => from.Key))
                .ForMember(x => x.Quantity, opt => opt.MapFrom(from => from.Value))
                .ForMember(x=>x.Order,opt=>opt.Ignore())
                .ForMember(x=>x.OrderId,opt=>opt.Ignore())
                .ForMember(x=>x.Product,opt=>opt.Ignore())
                .ForMember(x=>x.Id,opt=>opt.Ignore());

            Mapper.CreateMap<OrderDetail, ProductModel>()
                .ForMember(x => x.Category, opt => opt.MapFrom(from => from.Product.Category.Name))
                .ForMember(x => x.Description, opt => opt.MapFrom(from => from.Product.Description))
                .ForMember(x => x.Id, opt => opt.MapFrom(from => from.Product.Id))
                .ForMember(x => x.Name, opt => opt.MapFrom(from => from.Product.Name))
                .ForMember(x=>x.HasPhoto,opt=>opt.MapFrom(from=>from.Product.Photo!=null))
                .ForMember(x => x.Price, opt => opt.MapFrom(from => from.Product.Price))
                .ForMember(x => x.Quantity, opt => opt.MapFrom(from => from.Quantity));

            Mapper.CreateMap<Order, OrderModel>()
                .ForMember(x=>x.OrderStatus,opt=>opt.MapFrom(from=>from.OrderStatus.ToString()))
                .ForMember(x => x.User, opt => opt.MapFrom(from => string.Format("{0} {1} {2}", from.User.FirstName, from.User.SecondName, from.User.LastName)))
                .ForMember(x => x.RealizedBy, opt => opt.MapFrom(from => string.Format("{0} {1} {2}", from.RealizedBy.FirstName, from.RealizedBy.SecondName, from.RealizedBy.LastName)))
                .ForMember(x => x.Products, opt => opt.MapFrom(from => from.OrderDetails.Select(Mapper.Map<ProductModel>)))
                .ForMember(x => x.TotalValue,
                           opt => opt.MapFrom(from => from.OrderDetails.Sum(q => q.Quantity*q.Product.Price)));
        }
    }
   
}